package com.focusai.efairy.ui.activity.report;


import android.support.design.widget.TabLayout;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.ui.base.BaseFragment;
import com.focusai.efairy.ui.widget.DoubleDatePickerDialog;
import com.focusai.efairy.utils.DateUtils;
import com.focusai.efairy.utils.report.ReportUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * 头部筛选条件的基类
 */
public class ReportFilterBaseFragment extends BaseFragment implements View.OnClickListener {
    //筛选的类型
    private int[] filterDates = {R.string.today_short, R.string.week_short, R.string.month_short, R.string.season_short, R.string.year_short, R.string.custom_date};
    //    private int[] filterDates = {R.string.today_short, R.string.week_short, R.string.month_short, R.string.season_short, R.string.year_short};
    //当前筛选的类型
    private int currType;

    //筛选日期的开始时间
    private Calendar currStartTime;
    //筛选日期的结束时间
    private Calendar currEndTime;

    private TabLayout tabLayout;
    //展示当前日期范围的值
    public TextView txtCurrentDay;
    //前一天
    private TextView txtDayBefore;
    //后一天
    private TextView txtDayAfter;
    //自定义模式下的开始日期
    private TextView txtStartDate;
    //自定义模式下的结束日期
    private TextView txtEndDate;
    //自定义模式的整个view
    private LinearLayout linearDefinedDateFilter;
    //是否处于加载数据状态
    private boolean isLoading = false;
    //遮挡在日期筛选上的view，主要是为了防止在加载数据的时候不停的切换日期导致数据加载错乱
    private View coverView;
    private View rootView;

    public void initData() {
        currStartTime = Calendar.getInstance();
        currEndTime = Calendar.getInstance();
    }

    public String getStartDateStr() {
        return DateUtils.defaultFormat(currStartTime.getTime());
    }

    public String getEndDateStr() {
        return DateUtils.defaultFormat(currEndTime.getTime());
    }

    protected void initView(View view) {
        super.initView(view);
        rootView = view;
        coverView = rootView.findViewById(R.id.view_cover);

        txtCurrentDay = (TextView) rootView.findViewById(R.id.txt_day);
        txtDayBefore = (TextView) rootView.findViewById(R.id.txt_day_before);
        txtDayAfter = (TextView) rootView.findViewById(R.id.txt_day_after);
        txtEndDate = (TextView) rootView.findViewById(R.id.txt_end_date);
        txtStartDate = (TextView) rootView.findViewById(R.id.txt_start_date);
        tabLayout = (TabLayout) rootView.findViewById(R.id.report_filter_tablayout);

        linearDefinedDateFilter = (LinearLayout) rootView.findViewById(R.id.linear_defined_date_filter);
    }

    @Override
    protected void setDefaultValues() {
        super.setDefaultValues();
        for (int strResId : filterDates) {
            TabLayout.Tab tab = tabLayout.newTab().setText(strResId);
            tabLayout.addTab(tab);
        }

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                currType = tab.getPosition();
                //重置为当前时间
                currEndTime = Calendar.getInstance();
                resetCurrentTime(currType, 0);

                switch (tab.getPosition()) {
                    case 0://日
                    case 1://周
                    case 2://月
                    case 3://季
                    case 4://年
                        txtDayBefore.setText("前一" + tab.getText());
                        txtDayAfter.setText("后一" + tab.getText());
                        linearDefinedDateFilter.setVisibility(View.GONE);
                        break;
//                    case 3://季
//                        linearDefinedDateFilter.setVisibility(View.VISIBLE);
//                        txtStartDate.setText(DateUtils.defaultFormat(currStartTime.getTime()));
//                        txtEndDate.setText(DateUtils.defaultFormat(currEndTime.getTime()));
//                        txtDayBefore.setText("前一" + tab.getText());
//                        txtDayAfter.setText("后一" + tab.getText());
//                        break;
                    case 5://自定义
                        linearDefinedDateFilter.setVisibility(View.VISIBLE);
                        txtStartDate.setText(DateUtils.defaultFormat(currStartTime.getTime()));
                        txtEndDate.setText(DateUtils.defaultFormat(currEndTime.getTime()));
                        break;
                    default:
                        break;
                }
                //切换日期后调用加载数据的方法
                loadData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        resetCurrentTime(currType, 0);
    }

    @Override
    protected void bindEvents() {
        super.bindEvents();
        coverView.setOnClickListener(this);
        txtDayBefore.setOnClickListener(this);
        txtDayAfter.setOnClickListener(this);
        rootView.findViewById(R.id.linear_defined_date_filter).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_day_before:
                resetCurrentTime(currType, -1);
                loadData();
                break;
            case R.id.txt_day_after:
                resetCurrentTime(currType, 1);
                loadData();
                break;
            case R.id.linear_defined_date_filter:
                showDateDialog();
                break;
            case R.id.view_cover://主要是防止在加载过程中 切换日期 进行再次加载
                break;
        }
    }

    /**
     * 根据筛选类型来展示当前筛选的日期范围
     *
     * @param type
     * @return
     */
    private String getTitleTimeText(int type) {
        String currTime = "";
        switch (type) {
            case 0://日
                currTime = DateUtils.defaultFormat(currEndTime.getTime());
                break;
            case 1://周
                currTime = DateUtils.defaultFormat(currStartTime.getTime()) + " ~ " + DateUtils.defaultFormat(currEndTime.getTime());
                break;
            case 2://月
                currTime = currEndTime.get(Calendar.YEAR) + " 年 " + (currEndTime.get(Calendar.MONTH) + 1) + " 月";
                break;
            case 3://季
                currTime = DateUtils.defaultFormat(currStartTime.getTime()) + " ~ " + DateUtils.defaultFormat(currEndTime.getTime());
                break;
            case 4://年
                currTime = currEndTime.get(Calendar.YEAR) + " 年";
                break;
            default:
                break;

        }
        return currTime;
    }

    /**
     * 根据筛选类型来计算筛选的日期范围
     *
     * @param type                  时间范围类型
     * @param previous_current_next -1 上一个周期，0当日周期 ，1 下一个周期
     */
    private void resetCurrentTime(int type, int previous_current_next) {
        switch (type) {
            case 0://日
                if (previous_current_next == -1) {
                    DateUtils.getLastDayBeforField(Calendar.DAY_OF_YEAR, currEndTime, 1);
                } else if (previous_current_next == 1) {
                    DateUtils.getLastDayAfterField(Calendar.DAY_OF_YEAR, currEndTime, 1);
                }
                currStartTime = currEndTime;
                break;
            case 1://周

                if (previous_current_next == -1) {
                    DateUtils.getLastDayBeforField(Calendar.WEEK_OF_YEAR, currEndTime, 1);
                } else if (previous_current_next == 1) {
                    DateUtils.getLastDayAfterField(Calendar.WEEK_OF_YEAR, currEndTime, 1);
                }
                currStartTime.setTime(new Date(DateUtils.getMondayInWeek(currEndTime).getTimeInMillis()));
                currEndTime.setTime(new Date(DateUtils.getSundayInWeek(currEndTime).getTimeInMillis()));
                break;
            case 2://月
                if (previous_current_next == -1) {
                    DateUtils.getLastDayBeforField(Calendar.MONTH, currEndTime, 1);
                } else if (previous_current_next == 1) {
                    DateUtils.getLastDayAfterField(Calendar.MONTH, currEndTime, 1);
                }
                currStartTime = DateUtils.getFirstDayInMonth(currEndTime);
                currEndTime = DateUtils.getLastDayInMonth(currEndTime);
                break;
            case 3://季
                if (previous_current_next == -1) {
                    DateUtils.getLastDayBeforField(Calendar.MONTH, currEndTime, 3);
                } else if (previous_current_next == 1) {
                    DateUtils.getLastDayAfterField(Calendar.MONTH, currEndTime, 3);
                }
                currStartTime = DateUtils.getFirstDayBySeason(currEndTime);
                currEndTime = DateUtils.getLastDayBySeason(currEndTime);
                break;
            case 4://年
                if (previous_current_next == -1) {
                    DateUtils.getLastDayBeforField(Calendar.YEAR, currEndTime, 1);
                } else if (previous_current_next == 1) {
                    DateUtils.getLastDayAfterField(Calendar.YEAR, currEndTime, 1);
                }
                currStartTime = DateUtils.getFirstDayInYear(currEndTime);
                currEndTime = DateUtils.getLastDayInYear(currEndTime);
                break;
            default:
                break;
        }
        //显示对应日期
        txtCurrentDay.setText(getTitleTimeText(currType));
    }

    /**
     * 自定义日期范围时弹出的选择时间段框框
     */
    private void showDateDialog() {
        DoubleDatePickerDialog dialog = new DoubleDatePickerDialog(getContext(), 0, new DoubleDatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth, DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth) {
                StringBuffer sb = new StringBuffer();
                sb.append(startYear).append("-");
                sb.append(++startMonthOfYear < 10 ? "0" + startMonthOfYear : startMonthOfYear).append("-");
                sb.append(startDayOfMonth > 9 ? startDayOfMonth : "0" + startDayOfMonth);
                txtStartDate.setText(sb.toString());
                currStartTime.set(startYear, startMonthOfYear - 1, startDayOfMonth);
                sb = new StringBuffer();
                sb.append(endYear).append("-");
                sb.append(++endMonthOfYear < 10 ? "0" + endMonthOfYear : endMonthOfYear).append("-");
                sb.append(endDayOfMonth > 9 ? endDayOfMonth : "0" + endDayOfMonth);
                txtEndDate.setText(sb.toString());
                currEndTime.set(endYear, endMonthOfYear - 1, endDayOfMonth);
                loadData();
            }
        }, currStartTime.get(Calendar.YEAR), currStartTime.get(Calendar.MONTH), currStartTime.get(Calendar.DAY_OF_MONTH), currEndTime.get(Calendar.YEAR), currEndTime.get(Calendar.MONTH), currEndTime.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    public void loadData() {

    }

    public int getCurrType() {
        return currType;
    }

    /**
     * 日期转换成需要提交的参数类型，// 1-日报 2-周报 3-月报 4-季报 5-年报，传区间内的一个时间
     */
    public int getCurrDateType() {
        int dateType = 1;
        switch (getCurrType()) {
            case 0:
                dateType = ReportUtils.DAY;
                break;
            case 1:
                dateType = ReportUtils.WEEK;
                break;
            case 2:
                dateType = ReportUtils.MONDTH;

                break;
            case 3:
                dateType = ReportUtils.SESION;

                break;
            case 4:
                dateType = ReportUtils.YEAR;
                break;
            case 5:
                dateType = ReportUtils.CUSTOM;

                break;
            default:
                break;
        }
        return dateType;
    }

    public boolean isLoading() {
        return isLoading;
    }

    public void setIsLoading(boolean isLoading) {
        this.isLoading = isLoading;
        if (isLoading) {
            coverView.setVisibility(View.VISIBLE);
        } else {
            coverView.setVisibility(View.GONE);
        }
    }
}
